/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>Project: javase - Stream4
 * <p>Powered by webrx On 2021-12-22 16:04:52
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Stream4 {
    public static void main(String[] args) {
        var src = new File("C:\\javase\\10lambda\\src\\main\\java\\cn\\webrx\\Stream4.java");
        try (var read = new BufferedReader(new FileReader(src)); var read2 = new BufferedReader(new FileReader(src))) {
            var i = new AtomicInteger(5);
            //每行输出一行
            //read.lines().forEach(e -> System.out.printf("%02d、 %s%n", i.getAndIncrement(), e));
            //read.lines().skip(4).forEach(e -> System.out.printf("%02d、 %s%n", i.getAndIncrement(), e));
            //read.lines().skip(4).limit(5).forEach(e -> System.out.printf("%02d、 %s%n", i.getAndIncrement(), e));

            long rows = read2.lines().count();
            var n = new AtomicInteger(1);
            //read.lines().skip(rows - 5).forEach(e -> System.out.printf("%02d、 %s%n", i.getAndDecrement(), e));
            read.lines().map((e) -> String.format("%02d、%s%n", n.getAndIncrement(), e)).forEach(System.out::print);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test(String[] args) {
        Pattern p = Pattern.compile("[+]");
        String str = "1+2+3-5-1+9".replace("-", "+-");
        p.splitAsStream(str).forEach(System.out::println);

        var s = IntStream.range(1, 135);//串行流
        var s2 = s.parallel(); //返回并行流，多线程输出
        //s.forEach(System.out::println);
        s2.forEach(e -> {
            String tn = Thread.currentThread().getName();
            System.out.printf("%s:%d%n", tn, e);
        });

        var s10 = Stream.of(1, 2, 3);
        var s11 = Stream.generate(() -> "ok").limit(5);
        s11.forEach(System.out::println);
        //0 1 2
        //var s12 = Stream.iterate(0, (a) -> a + 1).limit(3);
        //1 3 5
        var s12 = Stream.iterate(1, (a) -> a + 2).limit(3);
        s12.forEach(System.out::println);
    }
}
